package fractal;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

import entity.Position;

public class Fractal2View extends BasicGame
{

	protected Position _pos;
	protected static int NBSCREEN = 128;
	protected static int TAILLECASE = 4;
	public static int DEPLACEMENT = 4;
	protected FractalTerrain _Map;

	public Fractal2View(String title)
	{
		super(title);
	}

	@Override
	public void render( GameContainer arg0 , Graphics g ) throws SlickException
	{
		// TODO Auto-generated method stub
		for (int i = _pos.getX(); i < _pos.getX() + NBSCREEN; i++)
		{
			for (int j = _pos.getY(); j < _pos.getY() + NBSCREEN; j++)
			{
				g.setColor(new Color(0, 0, 0));
				double hauteur = _Map.getAltitude(i, j) ;
				if (hauteur > 120)
				{
					g.setColor(new Color(126, 71, 6));
				}
				else if (hauteur > 0.9)
				{
					g.setColor(new Color(227, 131, 18));
				}
				else if (hauteur < -100)
				{
					g.setColor(new Color(30, 94, 131));
				}
				else if (hauteur < 0.6)
				{
					g.setColor(new Color(166, 202, 240));
				}
				else if (hauteur < 0.65)
				{
					g.setColor(new Color(248, 203, 132));
				}
				else
				{
					g.setColor(new Color(177, 253, 69));
				}

				int x = (i - _pos.getX()) * TAILLECASE;
				int y = (j - _pos.getY()) * TAILLECASE;
				Rectangle rectangle = new Rectangle(x, y, x + TAILLECASE, y + TAILLECASE);
				g.fill(rectangle);
			}
		}
	}

	@Override
	public void init( GameContainer pGC ) throws SlickException
	{

		//
		_pos = new Position(0, 0);

		_Map = new FractalTerrain(9, 0.1);

	}

	@Override
	public void update( GameContainer gc , int arg1 ) throws SlickException
	{
		Input input = gc.getInput();
		if (input.isKeyDown(Input.KEY_Z) && _pos.getY() - DEPLACEMENT >= 0)
		{
			_pos = new Position(_pos.getX(), _pos.getY() - DEPLACEMENT);
		}
		if (input.isKeyDown(Input.KEY_S) && _pos.getY() + DEPLACEMENT < _Map.size() - NBSCREEN)
		{
			_pos = new Position(_pos.getX(), _pos.getY() + DEPLACEMENT);
		}
		if (input.isKeyDown(Input.KEY_Q) && _pos.getX() - DEPLACEMENT >= 0)
		{
			_pos = new Position(_pos.getX() - DEPLACEMENT, _pos.getY());
		}
		if (input.isKeyDown(Input.KEY_D) && _pos.getX() + DEPLACEMENT < _Map.size() - NBSCREEN)
		{
			_pos = new Position(_pos.getX() + DEPLACEMENT, _pos.getY());
		}
	}

}
